<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'">
      <el-row>
        <el-col :span="12">
          <el-form-item label="推荐行程名称" prop="name">
            <el-input v-model="dataForm.name" placeholder="推荐行程名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="简介" prop="intro">
            <el-input v-model="dataForm.intro" placeholder="请输入简介" type="textarea" :rows="2"> </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="计划旅行天数" prop="planDayNum">
            <el-input-number v-model="dataForm.planDayNum" placeholder="计划旅行天数"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划游玩地点数" prop="planPlaceNum">
            <el-input-number v-model="dataForm.planPlaceNum" placeholder="计划游玩地点数"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="封面图" v-for="(image, index) in dataForm.imgObjList" :label="'封面图' + (index + 1)"
            :key="index" :prop="'imgObjList.' + index + '.value'" :rules="{
              required: true, message: '封面图不能为空', trigger: 'blur'
            }">
            <el-input v-model="image.value" placeholder="封面图"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-for="(image, index) in dataForm.imgObjList" :key="index">
            <el-button @click.prevent="removeImage(image)">删除</el-button>
            <el-button @click="addImage">新增封面</el-button>
          </el-form-item>

        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item v-for="(domain, index) in dataForm.tagsList" :label="'标签' + (index + 1)" :key="index"
            :prop="'tagsList.' + index + '.tagsId'" :rules="{
              required: true, message: '标签不能为空', trigger: 'blur'
            }">
            <el-select v-model="domain.tagsId" placeholder="请选择标签">
              <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
       
        <el-col :span="12">
          <el-form-item v-for="(domain, index) in dataForm.tagsList" :key="index">
            <!-- <el-color-picker v-model="domain.tagsColor">
            </el-color-picker> -->
            <el-button @click.prevent="removeDomain(domain)">删除</el-button>
            <el-button @click="addDomain">新增标签</el-button>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="路线" prop="routeIdList">
            <el-select v-model="dataForm.routeIdList" placeholder="请选择路线" multiple>
              <el-option v-for="item in routeOptions" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col>

        </el-col>
      </el-row>



    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data() {
    return {

      dynamicValidateForm: {
        domains: [{
          value: '',
          color: '#409EFF',
        }]
      },
      email: '',
      visible: false,
      dataForm: {
        imgObjList: [{ value: '' }],
        id: '',
        name: '',
        intro: '',
        planDayNum: '',
        planPlaceNum: '',
        coverImage: '',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: '',
        tagsList: [
          { tagsId: '', tagsColor: '#409EFF' }
        ],
        routeIdList: []
      },
      options: [],
      routeOptions: []

    }
  },
  mounted() {
    this.getTagsList()
    this.getRouteList()
  },

  computed: {
    dataRule() {
      return {
        name: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        intro: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        planDayNum: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        planPlaceNum: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        coverImage: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        creator: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createDate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updater: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updateDate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    getRouteList() {
      this.$http.post('/scenic/recroute/selectAll',).then(({ data: res }) => {
        console.log('ssssssss', res
        );
        if (res.msg == 'success') {
          this.routeOptions = res.data
        }
      })
    },
    removeImage(item) {
      var index = this.dataForm.imgObjList.indexOf(item)
      if (index !== -1) {
        this.dataForm.imgObjList.splice(index, 1)
      }
    },
    addImage() {
      this.dataForm.imgObjList.push({ value: '' });
    },
    getTagsList() {
      this.$http.post('/scenic/customtags/selectList').then(({ data: res }) => {
        if (res.msg == 'success') {
          this.options = res.data
        }
      })
    },
    removeDomain(item) {
      var index = this.dataForm.tagsList.indexOf(item)
      if (index !== -1) {
        this.dataForm.tagsList.splice(index, 1)
      }
    },
    addDomain() {
      this.dataForm.tagsList.push({
        tagsName: '',
        tagsColor: '#409EFF'
      });
    },
    init() {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/scenic/recjourney/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => { })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {

      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/scenic/recjourney/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
<style lang="scss" scoped>
/deep/ .el-form-item__content {
  display: flex;
}
</style>